import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;


import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import static java.lang.Thread.sleep;

public class Test2 {
    public static void main(String[] args) throws InterruptedException, IOException {
//        test1();
//        test2();
//        page();
//        page2();
//        page3();//下拉框的使用
//        page4();//弹窗
        page5();//上传文件
    }

    private static void page5() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/AutoTest20240723/Page/test05.html?_ijt=kr2bv0uvf6r6cimqci2s30ipu3&_ij_reload=RELOAD_ON_SAVE");
        sleep(3000);
        //上传文件
        webDriver.findElement(By.cssSelector("body > input[type=file]")).sendKeys("D:\\daeryoupan\\研究生\\chatgpt使用教程.docx");

    }

    private static void page4() throws InterruptedException {
        //操作弹窗按钮
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/AutoTest20240723/Page/test04.html?_ijt=9cvsekkf4mduqs32v3nfq0vnva&_ij_reload=RELOAD_ON_SAVE");
        sleep(3000);
        //找到”这是一个弹窗“并点击
        webDriver.findElement(By.cssSelector("body > button")).click();
        sleep(3000);
        //驱动切换到alert弹窗
        //点击弹窗里面的取消
        webDriver.switchTo().alert().dismiss();
        sleep(3000);
        //点击”这是一个弹窗“
        webDriver.findElement(By.cssSelector("body > button")).click();
        sleep(3000);
        //驱动切换到alert，输入”骆长春“
        webDriver.switchTo().alert().sendKeys("骆长春");
        sleep(3000);
        //点击弹窗力的确认
        webDriver.switchTo().alert().accept();
        //校验界面的文本是否是骆长春，如果是返回测试通过，否则输入测试不通过
        String name = "骆长春";
        sleep(3000);
        String name_result = webDriver.findElement(By.cssSelector("body > div:nth-child(5)")).getText();
        System.out.println(name_result);
        if (name_result.equals(name)){
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }
    }

    private static void page3() {
        //操作test03.html
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/AutoTest20240723/Page/test03.html?_ijt=uuqu2dgb613db9uoc9s8ubm42c&_ij_reload=RELOAD_ON_SAVE");
        //选中select元素
        WebElement webElement = webDriver.findElement(By.cssSelector("#ShippingMethod"));
        //选中下拉框的其他选项：
        //1.创建select对象
        Select select = new Select(webElement);
        //2.选择某个下拉框选项
        select.selectByIndex(2);//select里面的选项从0开始标号
    }

    private static void page2() throws InterruptedException {
        //操作test02.html
        // 切换iframe界面的操作
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/AutoTest20240723/Page/test02.html?_ijt=fba73o27carlavq55ifkafupl3&_ij_reload=RELOAD_ON_SAVE");
        //访问iframe下面的元素
        webDriver.switchTo().frame("f1");
        sleep(3000);
        webDriver.findElement(By.cssSelector("body > div > div > a")).click();
    }

    private static void page() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/AutoTest20240723/Page/test01.html?_ijt=nva2ia7svk186dgl94f0t4k1js&_ij_reload=RELOAD_ON_SAVE");
        List<WebElement> webElements = webDriver.findElements(By.cssSelector("input"));
        for (int i = 0; i < webElements.size(); i++) {
            if (webElements.get(i).getAttribute("type").equals("checkbox")){
                webElements.get(i).click();
            }
        }
    }

    private static void test2() throws IOException, InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("蛋糕");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //将界面进行截图，并将截的图放在file临时变量中，该临时变量是文件类型
        File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        //将file存到磁盘中
        FileUtils.copyFile(file,new File("D:\\daeryoupan\\比特课\\code\\测试\\Selenium2024_07_23\\截图20240725.png"));
        webDriver.quit();
    }

    private static void test1() throws InterruptedException {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //找到新闻元素，点击新闻
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
//        //在新闻界面找到输入框，输入经济学新闻
//        webDriver.findElement(By.cssSelector("#ww")).sendKeys("经济学新闻");//此时不会成功，因为当前页面和百度首页不在一个页面
        //需要通过以下过程切换窗口:
        //1.获取当前页面的句柄
        String cur_handle = webDriver.getWindowHandle();
        //2.获取所有页面的句柄
        Set<String> handles = webDriver.getWindowHandles();
        //3.遍历当前浏览器的句柄
        String target = "";
        for (String tmp:handles) {
            target = tmp;
        }
        //将4.窗口句柄切换为最后一个句柄
        webDriver.switchTo().window(target);
        //在新闻界面找到输入框，输入经济学新闻
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("经济学新闻");
        sleep(3000);
        //在新闻界面找到“百度一下”，点击
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
        sleep(3000);
        //获取当前页面与“经济学”相关的内容
        List<WebElement> results = webDriver.findElements(By.xpath("//em[text()=\"经济学\"]"));
        //校验是否搜索成功，如果返回的结果有“新闻”则通过测试，否则测试不通过
        int flag = 0;
        for (int i = 0; i < results.size(); i++) {
            if (results.get(i).getText().equals("经济学")){
                flag = 1;
                break;
            }
        }
        if (flag == 1){
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }
        webDriver.quit();
    }
}
